<script setup>
import { RouterLink } from 'vue-router'
import { useUserStore } from '../stores/user'

defineProps({
  isAuthenticated: Boolean,
  username: String
})

const userStore = useUserStore()

const handleLogout = async () => {
  try {
    await userStore.logout()
  } catch (error) {
    alert('Logout failed: ' + (error.message || 'Unknown error'))
  }
}
</script>

<template>
  <nav class="app-nav">
    <div class="nav-brand">
      <RouterLink to="/">Vue Demo</RouterLink>
    </div>
    
    <div class="nav-links">
      <RouterLink to="/">Home</RouterLink>
      <RouterLink v-if="!isAuthenticated" to="/login">Login</RouterLink>
      <RouterLink v-if="isAuthenticated" to="/profile">Profile</RouterLink>
      <button 
        v-if="isAuthenticated" 
        @click="handleLogout"
        class="logout-btn"
      >
        Logout ({{ username }})
      </button>
    </div>
  </nav>
</template>

<style scoped>
.app-nav {
  background: var(--primary-color);
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-brand a {
  color: white;
  font-size: 1.5rem;
  text-decoration: none;
  font-weight: bold;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
  align-items: center;
}

.nav-links a {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  padding: 0.5rem;
  transition: color 0.3s ease;
}

.nav-links a:hover {
  color: white;
}

.nav-links a.router-link-exact-active {
  color: var(--secondary-color);
  border-bottom: 2px solid var(--secondary-color);
}

.logout-btn {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--secondary-color);
}

@media (max-width: 768px) {
  .app-nav {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }
  
  .nav-links {
    flex-wrap: wrap;
    justify-content: center;
  }
}
</style>